package com.njupt.towersystem.controller;

import com.njupt.towersystem.domain.managerinfos;
import com.njupt.towersystem.services.ManagerService;
import com.njupt.towersystem.services.PowerService;
import com.njupt.towersystem.services.RegisterUserService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/sys")
@CrossOrigin("*")
public class ManagerInfoContrl {
    @Resource
    private ManagerService managerService;
    @Resource
    private RegisterUserService registerUserService;
    @Resource
    private PowerService powerService;

    @RequestMapping("/userreg")
    public String reg(managerinfos mi){
        boolean ret = registerUserService.regUser(mi);
        if (ret){
            return "{\"userrreg\":\"success\"}";

        }else {
            return "{\"userrreg\":\"fail\"}";

        }
    }
//    用户登录
    @RequestMapping("/login")
    public managerinfos login(
            @RequestParam(required = false) String loginname,
            @RequestParam(required = false) String loginpwd,
            @RequestParam(required = false) String token
            ){
       return managerService.login(loginname,loginpwd,token);


    }
    @RequestMapping("/userpower")
    public String setuserpower(
//            说明ruid并不是必须的参数
            @RequestParam(required = false) int ruid,
            int userid, int powerid,String isgrant){
        if (isgrant.equalsIgnoreCase("y")){
            powerService.grantUserRule(ruid,powerid,userid);
        }else {
            powerService.revokeUserRule(powerid,userid);
        }
        return "{\"powerset\":\"success\"}";
    }
//    添加注释
    @RequestMapping("/insertPower")
    public String regpower(int rulerid,String rulename){
        powerService.addNewRule(rulerid,rulename);
        return "{\"powerreg\":\"success\"}";
    }

}
